msg_tool\scripts\softpal\arc/
pac.rs

1//! Softpal PAC archive (.pac)
2use super::*;
3use crate::ext::io::*;
4use crate::scripts::base::*;
5use crate::types::*;
6use anyhow::{Result, anyhow, ensure};
7use std::io::{Read, Seek, SeekFrom};
8use std::sync::{Arc, Mutex};
9
10const SOFTPAL_INDEX_OFFSET: u64 = 0x3FE;
11const AMUSE_INDEX_OFFSET: u64 = 0x804;
12const XOR_KEY: u32 = 0xF7D5859D;
13
14#[derive(Debug, Clone, Copy)]
15enum SoftpalPacVariant {
16    Softpal,
17    Amuse,
18}
19
20#[derive(Debug)]
21/// Softpal PAC archive builder.
22pub struct SoftpalPacBuilder {
23    variant: SoftpalPacVariant,
24}
25
26impl SoftpalPacBuilder {
27    /// Creates a builder for the classic Softpal PAC layout.
28    pub fn new() -> Self {
29        Self {
30            variant: SoftpalPacVariant::Softpal,
31        }
32    }
33
34    /// Creates a builder for the Amuse Craft PAC layout.
35    pub fn new_amuse() -> Self {
36        Self {
37            variant: SoftpalPacVariant::Amuse,
38        }
39    }
40}
41
42impl ScriptBuilder for SoftpalPacBuilder {
43    fn default_encoding(&self) -> Encoding {
44        Encoding::Cp932
45    }
46
47    fn default_archive_encoding(&self) -> Option<Encoding> {
48        Some(Encoding::Cp932)
49    }
50
51    fn build_script(
52        &self,
53        buf: Vec<u8>,
54        _filename: &str,
55        _encoding: Encoding,
56        archive_encoding: Encoding,
57        config: &ExtraConfig,
58        _archive: Option<&Box<dyn Script>>,
59    ) -> Result<Box<dyn Script + Send + Sync>> {
60        Ok(Box::new(SoftpalPacArchive::new(
61            MemReader::new(buf),
62            archive_encoding,
63            config,
64            self.variant,
65        )?))
66    }
67
68    fn build_script_from_file(
69        &self,
70        filename: &str,
71        _encoding: Encoding,
72        archive_encoding: Encoding,
73        config: &ExtraConfig,
74        _archive: Option<&Box<dyn Script>>,
75    ) -> Result<Box<dyn Script + Send + Sync>> {
76        let file = std::fs::File::open(filename)?;
77        let reader = std::io::BufReader::new(file);
78        Ok(Box::new(SoftpalPacArchive::new(
79            reader,
80            archive_encoding,
81            config,
82            self.variant,
83        )?))
84    }
85
86    fn build_script_from_reader<'a>(
87        &self,
88        reader: Box<dyn ReadSeek + Send + Sync + 'a>,
89        _filename: &str,
90        _encoding: Encoding,
91        archive_encoding: Encoding,
92        config: &ExtraConfig,
93        _archive: Option<&Box<dyn Script>>,
94    ) -> Result<Box<dyn Script + Send + Sync + 'a>> {
95        Ok(Box::new(SoftpalPacArchive::new(
96            reader,
97            archive_encoding,
98            config,
99            self.variant,
100        )?))
101    }
102
103    fn extensions(&self) -> &'static [&'static str] {
104        &["pac"]
105    }
106
107    fn script_type(&self) -> &'static ScriptType {
108        match self.variant {
109            SoftpalPacVariant::Softpal => &ScriptType::SoftpalPac,
110            SoftpalPacVariant::Amuse => &ScriptType::SoftpalPacAmuse,
111        }
112    }
113
114    fn is_archive(&self) -> bool {
115        true
116    }
117
118    fn is_this_format(&self, _filename: &str, buf: &[u8], buf_len: usize) -> Option<u8> {
119        match self.variant {
120            SoftpalPacVariant::Softpal => None,
121            SoftpalPacVariant::Amuse => {
122                if buf_len >= 4 && buf.starts_with(b"PAC ") {
123                    Some(10)
124                } else {
125                    None
126                }
127            }
128        }
129    }
130}
131
132#[derive(Debug, Clone)]
133struct SoftpalPacEntry {
134    name: String,
135    offset: u32,
136    size: u32,
137}
138
139#[derive(Debug)]
140/// Softpal PAC archive reader.
141pub struct SoftpalPacArchive<'a, T: Read + Seek + std::fmt::Debug + 'a> {
142    reader: Arc<Mutex<T>>,
143    entries: Vec<SoftpalPacEntry>,
144    _mark: std::marker::PhantomData<&'a ()>,
145}
146
147impl<'a, T: Read + Seek + std::fmt::Debug + Send + Sync + 'a> SoftpalPacArchive<'a, T> {
148    fn new(
149        mut reader: T,
150        archive_encoding: Encoding,
151        _config: &ExtraConfig,
152        variant: SoftpalPacVariant,
153    ) -> Result<Self> {
154        let encoding = match archive_encoding {
155            Encoding::Auto => Encoding::Cp932,
156            other => other,
157        };
158        let file_len = reader.stream_length()?;
159        if let SoftpalPacVariant::Amuse = variant {
160            let signature = reader.peek_u32_at(0)?;
161            ensure!(
162                signature == 0x2043_4150,
163                "Invalid Softpal PAC/Amuse signature: {signature:08X}"
164            );
165        }
166
167        let count_offset = match variant {
168            SoftpalPacVariant::Softpal => 0,
169            SoftpalPacVariant::Amuse => 8,
170        };
171        let count = reader.peek_i32_at(count_offset)?;
172        ensure!(count >= 0, "Negative entry count: {count}");
173        let count = count as usize;
174
175        if count == 0 {
176            return Ok(Self {
177                reader: Arc::new(Mutex::new(reader)),
178                entries: Vec::new(),
179                _mark: std::marker::PhantomData,
180            });
181        }
182
183        let (index_offset, name_length) = match variant {
184            SoftpalPacVariant::Softpal => {
185                let mut chosen = None;
186                for &candidate in &[0x20usize, 0x10usize] {
187                    let first_offset =
188                        reader.peek_u32_at(SOFTPAL_INDEX_OFFSET + candidate as u64 + 4)? as u64;
189                    let expected = SOFTPAL_INDEX_OFFSET + (candidate as u64 + 8) * count as u64;
190                    if first_offset == expected {
191                        ensure!(
192                            first_offset <= file_len,
193                            "First entry offset {first_offset:#X} exceeds archive length {file_len:#X}"
194                        );
195                        chosen = Some((SOFTPAL_INDEX_OFFSET, candidate));
196                        break;
197                    }
198                }
199                chosen.ok_or_else(|| anyhow!("Unsupported Softpal PAC layout"))?
200            }
201            SoftpalPacVariant::Amuse => {
202                let name_length = 0x20usize;
203                let first_offset =
204                    reader.peek_u32_at(AMUSE_INDEX_OFFSET + name_length as u64 + 4)? as u64;
205                let expected = AMUSE_INDEX_OFFSET + (name_length as u64 + 8) * count as u64;
206                ensure!(
207                    first_offset == expected,
208                    "Invalid Softpal PAC/Amuse index layout: expected {expected:#X}, got {first_offset:#X}"
209                );
210                ensure!(
211                    first_offset <= file_len,
212                    "First entry offset {first_offset:#X} exceeds archive length {file_len:#X}"
213                );
214                (AMUSE_INDEX_OFFSET, name_length)
215            }
216        };
217
218        reader.seek(SeekFrom::Start(index_offset))?;
219        let mut entries = Vec::with_capacity(count);
220        for _ in 0..count {
221            let name = reader.read_fstring(name_length, encoding, true)?;
222            let size = reader.read_u32()?;
223            let offset = reader.read_u32()?;
224            let end = offset as u64 + size as u64;
225            ensure!(
226                end <= file_len,
227                "Entry '{name}' exceeds archive bounds: offset={offset:#X}, size={size:#X}"
228            );
229            entries.push(SoftpalPacEntry { name, offset, size });
230        }
231
232        Ok(Self {
233            reader: Arc::new(Mutex::new(reader)),
234            entries,
235            _mark: std::marker::PhantomData,
236        })
237    }
238}
239
240impl<'b, T: Read + Seek + std::fmt::Debug + Send + Sync + 'b> Script for SoftpalPacArchive<'b, T> {
241    fn default_output_script_type(&self) -> OutputScriptType {
242        OutputScriptType::Json
243    }
244
245    fn default_format_type(&self) -> FormatOptions {
246        FormatOptions::None
247    }
248
249    fn is_archive(&self) -> bool {
250        true
251    }
252
253    fn iter_archive_filename<'a>(
254        &'a self,
255    ) -> Result<Box<dyn Iterator<Item = Result<String>> + 'a>> {
256        Ok(Box::new(
257            self.entries.iter().map(|entry| Ok(entry.name.clone())),
258        ))
259    }
260
261    fn iter_archive_offset<'a>(&'a self) -> Result<Box<dyn Iterator<Item = Result<u64>> + 'a>> {
262        Ok(Box::new(
263            self.entries.iter().map(|entry| Ok(entry.offset as u64)),
264        ))
265    }
266
267    fn open_file<'a>(&'a self, index: usize) -> Result<Box<dyn ArchiveContent + Send + Sync + 'a>> {
268        let entry = self
269            .entries
270            .get(index)
271            .ok_or_else(|| anyhow!("Index out of bounds: {index}"))?;
272        let mut buf = [0u8; 16];
273        let buflen = self.reader.cpeek_at(entry.offset as u64, &mut buf)?;
274        let script_type = detect_script_type(&entry.name, &buf[..buflen]);
275        if buflen >= 16 && should_decrypt_entry(&buf) {
276            let mut data = vec![0u8; entry.size as usize];
277            self.reader.cpeek_exact_at(entry.offset as u64, &mut data)?;
278            decrypt_entry(&mut data);
279            Ok(Box::new(MemEntry::new(
280                entry.name.clone(),
281                data,
282                script_type,
283            )))
284        } else {
285            Ok(Box::new(PacEntry::new(
286                entry.clone(),
287                self.reader.clone(),
288                script_type,
289            )))
290        }
291    }
292}
293
294fn should_decrypt_entry(data: &[u8]) -> bool {
295    data.len() > 16 && data[0] == b'$'
296}
297
298fn decrypt_entry(data: &mut [u8]) {
299    if data.len() <= 16 {
300        return;
301    }
302    let mut shift: u32 = 4;
303    for chunk in data[16..].chunks_exact_mut(4) {
304        let mut block = [0u8; 4];
305        block.copy_from_slice(chunk);
306        let rotate = (shift & 7) as u32;
307        block[0] = block[0].rotate_left(rotate);
308        shift = shift.wrapping_add(1);
309        let decrypted = u32::from_le_bytes(block) ^ XOR_KEY;
310        chunk.copy_from_slice(&decrypted.to_le_bytes());
311    }
312}
313
314#[derive(Debug)]
315struct MemEntry {
316    name: String,
317    data: Vec<u8>,
318    pos: usize,
319    script_type: Option<ScriptType>,
320}
321
322impl MemEntry {
323    pub fn new(name: String, data: Vec<u8>, script_type: Option<ScriptType>) -> Self {
324        Self {
325            name,
326            data,
327            pos: 0,
328            script_type,
329        }
330    }
331}
332
333impl ArchiveContent for MemEntry {
334    fn name(&self) -> &str {
335        &self.name
336    }
337
338    fn size(&self) -> Option<u64> {
339        Some(self.data.len() as u64)
340    }
341
342    fn script_type(&self) -> Option<&ScriptType> {
343        self.script_type.as_ref()
344    }
345
346    fn data(&mut self) -> Result<Vec<u8>> {
347        Ok(self.data.clone())
348    }
349
350    fn to_data<'a>(&'a mut self) -> Result<Box<dyn ReadSeek + Send + Sync + 'a>> {
351        Ok(Box::new(MemReaderRef::new(&self.data)))
352    }
353}
354
355impl Read for MemEntry {
356    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
357        if self.pos >= self.data.len() {
358            return Ok(0);
359        }
360        let bytes_to_read = buf.len().min(self.data.len() - self.pos);
361        if bytes_to_read == 0 {
362            return Ok(0);
363        }
364        buf[..bytes_to_read].copy_from_slice(&self.data[self.pos..self.pos + bytes_to_read]);
365        self.pos += bytes_to_read;
366        Ok(bytes_to_read)
367    }
368}
369
370impl Seek for MemEntry {
371    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
372        let len = self.data.len() as i64;
373        let current = self.pos as i64;
374        let new_pos = match pos {
375            SeekFrom::Start(offset) => offset as i64,
376            SeekFrom::End(offset) => len + offset,
377            SeekFrom::Current(offset) => current + offset,
378        };
379        if new_pos < 0 || new_pos > len {
380            return Err(std::io::Error::new(
381                std::io::ErrorKind::InvalidInput,
382                "Seek position is out of bounds",
383            ));
384        }
385        self.pos = new_pos as usize;
386        Ok(self.pos as u64)
387    }
388
389    fn stream_position(&mut self) -> std::io::Result<u64> {
390        Ok(self.pos as u64)
391    }
392}
393
394#[derive(Debug)]
395struct PacEntry<T: Read + Seek + std::fmt::Debug> {
396    header: SoftpalPacEntry,
397    pos: u64,
398    reader: Arc<Mutex<T>>,
399    script_type: Option<ScriptType>,
400}
401
402impl<T: Read + Seek + std::fmt::Debug> PacEntry<T> {
403    fn new(
404        header: SoftpalPacEntry,
405        reader: Arc<Mutex<T>>,
406        script_type: Option<ScriptType>,
407    ) -> Self {
408        Self {
409            header,
410            pos: 0,
411            reader,
412            script_type,
413        }
414    }
415}
416
417impl<T: Read + Seek + Send + Sync + std::fmt::Debug> ArchiveContent for PacEntry<T> {
418    fn name(&self) -> &str {
419        &self.header.name
420    }
421
422    fn size(&self) -> Option<u64> {
423        Some(self.header.size as u64)
424    }
425
426    fn script_type(&self) -> Option<&ScriptType> {
427        self.script_type.as_ref()
428    }
429
430    fn to_data<'a>(&'a mut self) -> Result<Box<dyn ReadSeek + Send + Sync + 'a>> {
431        Ok(Box::new(self))
432    }
433}
434
435impl<T: Read + Seek + std::fmt::Debug> Read for PacEntry<T> {
436    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
437        if self.pos >= self.header.size as u64 {
438            return Ok(0);
439        }
440        let bytes_to_read = buf.len().min((self.header.size as u64 - self.pos) as usize);
441        if bytes_to_read == 0 {
442            return Ok(0);
443        }
444        let bytes_read = self.reader.cpeek_at(
445            self.header.offset as u64 + self.pos,
446            &mut buf[..bytes_to_read],
447        )?;
448        self.pos += bytes_read as u64;
449        Ok(bytes_read)
450    }
451}
452
453impl<T: Read + Seek + std::fmt::Debug> Seek for PacEntry<T> {
454    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
455        let len = self.header.size as i64;
456        let current = self.pos as i64;
457        let new_pos = match pos {
458            SeekFrom::Start(offset) => offset as i64,
459            SeekFrom::End(offset) => len + offset,
460            SeekFrom::Current(offset) => current + offset,
461        };
462        if new_pos < 0 || new_pos > len {
463            return Err(std::io::Error::new(
464                std::io::ErrorKind::InvalidInput,
465                "Seek position is out of bounds",
466            ));
467        }
468        self.pos = new_pos as u64;
469        Ok(self.pos as u64)
470    }
471
472    fn stream_position(&mut self) -> std::io::Result<u64> {
473        Ok(self.pos as u64)
474    }
475}